package com.ljkj.cordial.chat.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.util.glide.GlideUtil;
import com.nic.view.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/8/28.
 */

public class AvatarHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<FriendTable> urlList = new ArrayList<>();


    private Context context;

    public AvatarHorizontalAdapter(Context context) {
        this.context = context;
    }

    public void setUrlList(List<FriendTable> urlList) {
        if (urlList != null && !urlList.isEmpty()) {
            this.urlList = urlList;
        }
        notifyDataSetChanged();
    }

    public List<FriendTable> getUrlList() {
        return urlList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_avatar, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final FriendTable item = urlList.get(position);
            if (!TextUtils.isEmpty(item.getFriendAvatar())) {
                AlbumManageUtil.getInstance().downLoadFileFriend(item.getFriendAvatar(), item.getFriendUserId(), viewHolder.ivImage);
            } else {
                GlideUtil.getInstance().clear(viewHolder.ivImage);
                viewHolder.ivImage.setImageResource(R.mipmap.pic_user_avatar_default_36dp);

            }
            viewHolder.ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onImageClickListener != null) {
                        onImageClickListener.onClick(v, item, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return urlList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView ivImage;

        ViewHolder(View view) {
            super(view);
            ivImage = view.findViewById(R.id.item_img);
        }
    }

    private OnImageClickListener onImageClickListener;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        this.onImageClickListener = onImageClickListener;
    }

    public interface OnImageClickListener {
        void onClick(View v, FriendTable item, int position);
    }
}
